from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import  ModelViewSet
from .filter import Provincialfilter
from .models import Provincial, AreaManage, Image
from .serializer import ProvincialSerializer, UserSerializer, AreaSerializer, ImageSerializer


class ImageView(ModelViewSet):
    queryset = Image.objects.all()
    serializer_class = ImageSerializer




# Create your views here.
class AreaView(ModelViewSet):
    queryset = AreaManage.objects.all()
    serializer_class = AreaSerializer





class UserView(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def create(self, request, *args, **kwargs):
        Respons = super().create(request, *args, **kwargs)
        return Response({"status": '添加成功', 'code': '200', 'data': Respons.data})


    def update(self, request, *args, **kwargs):
        Respons = super().update(request, *args, **kwargs)
        return Response({"status": '修改成功', 'code': '200', 'data': Respons.data})

    def perform_create(self, serializer):
        serializer.save()

    def perform_update(self, serializer):
        serializer.save()


class ProvincialView(ModelViewSet):
    queryset = Provincial.objects.all()
    serializer_class = ProvincialSerializer
    filter_class = Provincialfilter

    ##增加
    def create(self, request, *args, **kwargs):
        Respons = super().create(request, *args, **kwargs)
        return Response({"status":'添加成功','code':'200','data':Respons.data})

    # #删除
    def destroy(self,request, *args ,**kwargs):
        Respons = super().destroy(request,*args ,**kwargs)
        return Response({"status": '删除成功', "code": "200"})

    # # 修改
    def update(self,request, *args, **kwargs):
        Respons = super().update(request,*args ,**kwargs)
        return Response({"status": '修改成功', 'code': '200', 'data': Respons.data})

    # 查找
    def list(self, request, *args, **kwargs):
        Respons = super().list(request, *args, **kwargs)
        return Response({"status": '查找成功', 'code': '200', "data": Respons.data})


    def perform_create(self, serializer):
        # serializer.save(password= make_password(self.request.data['password'], None, 'pbkdf2_sha256'))
        serializer.save(user=self.request.user)

    def perform_update(self, serializer):
        serializer.save(user=self.request.user)


